package com.zccx.eth.rest;

import com.zccx.eth.model.User;
import com.zccx.eth.service.IUserService;
import com.zccx.eth.util.Result;
import com.zccx.eth.util.object.StringUtils;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@Log4j
@RestController
public class LoginController {
    @Autowired
    private IUserService userService;

    @PostMapping("/login/login")
    public Result addProject(@RequestBody User info){
        try{
            if(StringUtils.isEmpty(info.getUserName())){
                return new Result(false,"用户名不能为空");
            }
            if(StringUtils.isEmpty(info.getPassword())){
                return new Result(false,"密码不能为空");
            }
            User user =userService.findByName(info.getUserName());
            if(user == null){
                return new Result(false,"没有此用户");
            }
            if(!info.getPassword().equals(user.getPassword())){
                return new Result(false,"密码错误");

            }
            user.setToken(UUID.randomUUID().toString().replaceAll("-",""));
            return new Result(user);
        }catch (Exception e){
            log.error("保存信息失败",e);
            return new Result(false,"保存信息失败");
        }
    }
}
